/*
Package cmd
@Author   : Uyynot
@Email    : uyynot@qq.com
@Time     : 2024/4/16 16:50
@File     : migrate.go
@Project  : gohub
@Desc     :
*/
package cmd

import (
	"github.com/spf13/cobra"
	"gohub/database/migrations"
	"gohub/pkg/migrate"
)

var CmdMigrate = &cobra.Command{
	Use:   "migrate",
	Short: "Run database migrations",
	// 所有migrate下的子命令都会执行以下代码
}

var CmdMigrateUp = &cobra.Command{
	Use:   "up",
	Short: "Run all available migrations",
	Run:   runUp,
}

func init() {
	CmdMigrate.AddCommand(CmdMigrateUp, CmdMigrateRollback, CmdMigrateReset, CmdMigrateRefresh, CmdMigrateFresh)
}

func migrator() *migrate.Migrator {
	//注册database/migrations下的所有迁移文件
	migrations.Initialize()
	//初始化migrator
	return migrate.NewMigrator()
}

func runUp(cmd *cobra.Command, args []string) {
	migrator().Up()
}

var CmdMigrateRollback = &cobra.Command{
	Use: "down",
	//设置别名，migrate down == migrate rollback
	Aliases: []string{"rollback"},
	Short:   "Reverse the up command",
	Run:     runDown,
}

func runDown(cmd *cobra.Command, args []string) {
	migrator().Rollback()
}

var CmdMigrateReset = &cobra.Command{
	Use:   "reset",
	Short: "Reset all migrations",
	Run:   runReset,
}

func runReset(cmd *cobra.Command, args []string) {
	migrator().Reset()
}

var CmdMigrateRefresh = &cobra.Command{
	Use:   "refresh",
	Short: "Reset and re-run all migrations",
	Run:   runRefresh,
}

func runRefresh(cmd *cobra.Command, args []string) {
	migrator().Refresh()
}

var CmdMigrateFresh = &cobra.Command{
	Use:   "fresh",
	Short: "Drop all tables and re-run all migrations",
	Run:   runFresh,
}

func runFresh(cmd *cobra.Command, args []string) {
	migrator().Fresh()
}
